package com.atom.module.document.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageView;

import com.atom.module.logger.Logger;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import io.reactivex.Observer;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: DocumentManager
 * @Description:
 * @author: wuyongzheng
 * @date: 2022/3/28
 * @Copyright: 2022/3/28 www.rongdasoft.com Inc. All rights reserved.
 */
public class DocumentHelper {

    public static final String document_path_param = "document_path_param";
    public static final String document_name_param = "document_name_param";
    public static final String document_check_history_param = "document_check_history_param";
    public static final String camera_model_param = "camera_model_param";
    public static final String camera_result_code_param = "camera_result_code_param";
    public static final String camera_result_value_param = "camera_result_value_param";
    public static final String camera_result_array_param = "camera_result_array_param";


    public static final String default_document_file = ".document";
    public static final String source_file = "source_file";
    public static final String handle_file = "handle_file";

    public static void imageShow(Context context, Bitmap bitmap) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AppCompatImageView imageView = new AppCompatImageView(context);
        imageView.setImageBitmap(bitmap);
        builder.setView(imageView);
        builder.setPositiveButton("close", (dialog, which) -> {
            dialog.dismiss();
            bitmap.recycle();
        });
        builder.create().show();
    }

    public static void documentDeleteShow(Context context, Observer<Boolean> observer) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("删除提醒");
        builder.setMessage("该照片即将被删除,是否确认删除");
        builder.setPositiveButton("sure", (dialog, which) -> {
            observer.onNext(true);
        });
        builder.setNegativeButton("cancel", (dialog, which) -> {
            observer.onNext(false);
        });
        builder.create().show();
    }

    //--------------------------------------

    public static String defaultDocumentName() {
        return String.format("荣大智慧云_%s", System.currentTimeMillis());
    }

    public static String getDocumentRootPath(Context context) {
        File filesDir = new File(context.getFilesDir(), "document_folder");
        if (filesDir.exists()) {
            return filesDir.getAbsolutePath();
        }
        if (filesDir.mkdirs()) {
            return filesDir.getAbsolutePath();
        }
        throw new RuntimeException("create document_folder in FilesDir error");
    }

    public static String getDocumentPath(Context context, String name) {
        String filesDirPath = getDocumentRootPath(context);
        if (TextUtils.isEmpty(name)) {
            name = default_document_file;
        }
        File file = new File(filesDirPath, name);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        if (file.mkdirs()) {
            return file.getAbsolutePath();
        }
        throw new RuntimeException("getDocumentPath by " + name + " is error");
    }

    public static String getDocumentSourcePath(Context context, String name) {
        return getOrCreateFolder(getDocumentPath(context, name), source_file, true);
    }

    public static String getDocumentSourcePath(String documentPath) {
        return getOrCreateFolder(documentPath, source_file, true);
    }

    public static String getDocumentHandlePath(Context context, String name) {
        return getOrCreateFolder(getDocumentPath(context, name), handle_file, true);
    }

    public static String getOrCreateFolder(String parentPath, String folder, boolean isFolder) {
        File file = new File(parentPath, folder);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        if (isFolder) {
            if (file.mkdirs()) {
                return file.getAbsolutePath();
            }
        } else {
            try {
                if (file.createNewFile()) {
                    return file.getAbsolutePath();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        throw new RuntimeException("create source_file is error");
    }

    public static String createFileByUUID(String parentPath) {
        return getOrCreateFolder(parentPath, UUID.randomUUID().toString()+".jpeg", false);
    }

    public static File createFile(String path, boolean isDelete) {
        if (TextUtils.isEmpty(path))
            throw new RuntimeException("current create file path is empty");
        File file = new File(path);
        if (file.exists()) {
            if (isDelete) {
                file.delete();
            } else {
                return file;
            }
        }
        File parentFile = file.getParentFile();
        if (parentFile == null) {
            throw new RuntimeException("parentFile file is null");
        }
        parentFile.mkdirs();
        return file;
    }

    public static boolean deleteFile(String singleFilePath) {
        if (TextUtils.isEmpty(singleFilePath)) return false;
        File file = new File(singleFilePath);
        if (!file.exists()) return false;
        if (!file.isFile()) return false;
        try {
            return file.delete();
        } catch (Exception e) {
            Logger.e(e);
            return false;
        }
    }
}

